package com.tangyuan.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangyuan.common.result.Result;
import com.tangyuan.dto.CouponFormDTO;
import com.tangyuan.model.Coupon;
import com.tangyuan.model.CouponExchangeCode;
import com.tangyuan.model.User;
import com.tangyuan.model.UserCoupon;
import com.tangyuan.common.util.CommStatus;
import com.tangyuan.user.mapper.UserCouponMapper;
import com.tangyuan.user.service.CouponExchangeCodeService;
import com.tangyuan.user.service.CouponService;
import com.tangyuan.user.service.UserCouponService;
import com.tangyuan.vo.UserCouponVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券的规则信息 控制器
 * </p>
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user/coupons")
public class CouponController {

    @Autowired
    private CouponService couponService;
    @Autowired
    private CommStatus commStatus;
    @Autowired
    private CouponExchangeCodeService codeService;
    @Autowired
    private UserCouponService userCouponService;

    @Autowired
    private UserCouponMapper userCouponMapper;


    // "新增优惠券接口"
    @PostMapping
    public Result<Object> saveCoupon(@RequestBody CouponFormDTO dto) {
        return couponService.saveCoupon(dto);
    }

    // "领取优惠券接口
    @GetMapping("/issue")
    public Result<Object> beginIssue(@RequestHeader(name = "Authorization",required = false) String token, @RequestParam (value = "id") Long id) {
        User user = CommStatus.getUser(token);
        return couponService.beginIssue(user, id);
    }


    /**
     * 查询可领取优惠券列表
     *
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/getCoupons")
    public Result<Object> getCoupons(@RequestHeader(name = "userId",required = false) Long userId,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Coupon::getIsDeleted, 0);
        queryWrapper.le(Coupon::getIssueBeginTime, new Date());
        queryWrapper.ge(Coupon::getIssueEndTime, new Date());
        queryWrapper.orderByDesc(Coupon::getCreateTime);
        List<Coupon> list = couponService.list(queryWrapper);
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(list);
        couponPageInfo.getList().forEach(item -> {
            item.setIsReceive(0);
            LambdaQueryWrapper<CouponExchangeCode> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CouponExchangeCode::getCouponId, item.getId());
            wrapper.eq(CouponExchangeCode::getUserId, userId);
            List<CouponExchangeCode> codeList = codeService.list(wrapper);
            if (!codeList.isEmpty()) {
                item.setIsReceive(1);
            }
        });
        return Result.ok(couponPageInfo.getList());
    }

    /**
     * 查询我的优惠券列表
     *
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/getMyCoupons")
    public Result<Object> getMyCoupons(@RequestHeader(name = "userId",required = false) Long userId, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);

        List<UserCouponVo> userCouponlist= userCouponMapper.selectByUserId(userId);

        return Result.ok(userCouponlist);
    }

    /**
     * 核销优惠券
     *
     * @param code
     * @return
     */
    @GetMapping("/writeoffCoupon")
    public Result<Object> writeOffCoupon(@RequestHeader(name = "userId",required = false) Long userId, @RequestParam(value = "code") String code) {
        return couponService.writeOffCoupon(userId, code);
    }

    /**
     * 根据优惠券code查询优惠券
     *
     * @param code
     * @return
     */
    @GetMapping("/getCouponByCode")
    public Result<Object> getCouponByCode(@RequestHeader(name = "userId",required = false) Long userId, @RequestParam(value = "code") String code) {
        LambdaQueryWrapper<CouponExchangeCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponExchangeCode::getUserId, userId);
        wrapper.eq(CouponExchangeCode::getCode, code);
        CouponExchangeCode exchangeCode = codeService.getOne(wrapper);
        exchangeCode.setCouponName(couponService.getById(exchangeCode.getCouponId()).getName());
        LambdaQueryWrapper<UserCoupon> userCouponWrapper = new LambdaQueryWrapper<>();
        userCouponWrapper.eq(UserCoupon::getCode, exchangeCode.getCode());
        userCouponWrapper.eq(UserCoupon::getUserId, userId);
        exchangeCode.setUserCoupon(userCouponService.getOne(userCouponWrapper));
        return Result.ok(exchangeCode);
    }
}
